<?php

namespace app\api\controller;
use app\common\controller\Api;
use think\Validate;
use think\Loader;
use think\Page;
use think\Db;
use \app\admin\model\HelpTask;
use \app\admin\model\HelpCategory;
use \app\admin\model\HelpName;
class Help extends Api
{ 
    protected $noNeedLogin = ['getHelpCategoryList', 'getHelpList', 'getHelpInfo'];
    protected $noNeedRight = '*';
    protected $user_id = 0;

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
    }

    /**
     * @MethodName 獲取互助廣場分類
     * @Author     Allen
     * @DateTime   2021-10-04T14:59:51+0800
     * @param      string
     * @return     [type]
     */
    public function getHelpCategoryList($value='')
    {
        $model = new HelpCategory;
        $list = $model
            ->field('id,image,name,createtime')
            ->order('weigh desc,id desc')
            ->select();

        $this->success('請求成功',$list);
    }

    /**
     * @MethodName 獲取求助標題列表
     * @Author     Allen
     * @DateTime   2021-10-04T14:59:51+0800
     * @param      string
     * @return     [type]
     */
    public function getHelpNameList($value='')
    {
        $model = new HelpName;
        $list = $model
            ->field('id,name,createtime')
            ->order('weigh desc,id desc')
            ->select();

        $this->success('請求成功',$list);
    }

    /**
     * @MethodName 獲取互助廣場列表
     * @Author     Allen
     * @DateTime   2021-10-04T14:55:12+0800
     * @param      string
     * @return     [array]
     */
    public function getHelpList($value='')
    {
        $cat_id = $this->request->post("cat_id", 0);
        $ky = $this->request->post("ky", '');
        $where = ['h.status'=>'3'];
        if ( $cat_id ) {
            $where['h.cat_id'] = $cat_id;
        }
        if ( $ky ) {
            $where['n.name'] = ['like',"%$ky%"];
        }
        $model = new HelpTask;
        $total = $model
            ->alias('h')
            ->where($where)
            ->join('user u','u.id = h.user_id')
            ->join('help_name n','n.id = h.help_name_id','left')
            ->count();

        $page = new Page($total,10);

        $list = $model
            ->alias('h')
            ->field('h.id,u.nickname,u.avatar,n.name,h.createtime')
            ->join('user u','u.id = h.user_id')
            ->join('help_name n','n.id = h.help_name_id','left')
            ->where($where)
            ->limit($page->firstRow.','.$page->listRows)
            ->order('h.id desc')
            ->select();

        foreach ($list as $key => &$value) {
            $value['collection_num'] = db('collection')
                                        ->where(['type'=>'4','relation_id'=>$value['id']])
                                        ->count();
        }
        
        unset($value);

        if ( $this->user_id ) {
            // 存儲用戶瀏覽記錄
            setArticleHistory($this->user_id,5,0);
        }

        $data = ['total'=>$total,'listrows'=>$page->listRows,'totalpages'=>$page->totalPages,'data'=>$list];
        $this->success('請求成功',$data);
    }

    /**
     * @MethodName 獲取互助廣場詳情
     * @Author     Allen
     * @DateTime   2021-10-04T15:42:20+0800
     * @return     [type]
     */
    public function getHelpInfo()
    {
        $rq_data = $this->request->param();
        $validate = Loader::validate('Article');
        if (!$validate->scene('info')->check($rq_data)) {
            $this->error($validate->getError());
        }

        $where = ['h.id'=>$rq_data['id'],'h.status'=>['not in',['1','2']]];
        $model = new HelpTask;
        $info = $model
            ->alias('h')
            ->field('h.id,u.nickname,u.avatar,n.name,h.video,h.content,h.createtime')
            ->join('user u','u.id = h.user_id')
            ->join('help_name n','n.id = h.help_name_id','left')
            ->where($where)
            ->find();

        if ( $info ) {
            $info['images'] = db('upload_images')
                            ->where(['type'=>'3','relation_id'=>$info['id']])
                            ->column('images');

            $model
            ->where(['id'=>$rq_data['id']])
            ->setInc('click');

            $info['collection_num'] = db('collection')
                                    ->where(['type'=>'4','relation_id'=>$info['id']])
                                    ->count();

            $info['collection_status'] = db('collection')
                                        ->where(['type'=>'4','relation_id'=>$info['id'],'user_id'=>$this->user_id])
                                        ->count();
            if ( $this->user_id ) {
                // 存儲用戶瀏覽記錄
                setArticleHistory($this->user_id,5,$info['id']);
            }
        }

        $this->success('請求成功',$info?$info:[]);
    }

    /**
     * @MethodName 互助廣場操作收藏
     * @Author     Allen
     * @DateTime   2021-10-04T16:13:20+0800
     * @param      string
     */
    public function setHelpCollection($value='')
    {
        $rq_data = $this->request->param();
        $validate = Loader::validate('Article');
        if (!$validate->scene('info')->check($rq_data)) {
            $this->error($validate->getError());
        }

        $where = ['h.id'=>$rq_data['id'],'h.status'=>['not in',['1','2']]];
        $model = new HelpTask;
        $info = $model
            ->alias('h')
            ->field('h.id,u.nickname,u.avatar,n.name,h.video,h.content,h.createtime')
            ->join('user u','u.id = h.user_id')
            ->join('help_name n','n.id = h.help_name_id','left')
            ->where($where)
            ->find();

        if ( !$info ) {
            $this->error('操作失敗');
        }
        $collection_status = db('collection')
                            ->where(['type'=>'4','relation_id'=>$rq_data['id'],'user_id'=>$this->user_id])
                            ->count();

        if ( $collection_status ) {
            $status = db('collection')
                    ->where(['type'=>'4','relation_id'=>$rq_data['id'],'user_id'=>$this->user_id])
                    ->delete();
        }else{
            $insert_arr = [];
            $insert_arr['user_id'] = $this->user_id;
            $insert_arr['type'] = '4';
            $insert_arr['relation_id'] = $rq_data['id'];
            $insert_arr['createtime'] = time();
            $status = db('collection')->insert($insert_arr);
        }
        if ( !$status ) {
            $this->error('操作失敗');
        }

        $this->success('操作成功');
    }

}